<?php
    date_default_timezone_set('UTC');
?>
<div class="well" >
    <div class="row-fluid centrar">
        <h3>Tipos de Gastos</h3>
        <input type="hidden" name="id" value=""/>
    </div><br/>
    <div class="contenido contexto">
        <div id="grid"></div>
    </div>
    <br/>      
</div>
<script type="text/x-kendo-template" id="template">
    <div class="toolbar contexto">
        <input type="button" class="k-button" name="btnNew" data-bind="click: new" value="Nuevo"/>
        <!--input type="button" class="k-button" name="btnEdit" data-bind="click: edit" value="Editar"/>
        <input type="button" class="k-button" name="btnDelete" data-bind="click: delete" value="Eliminar"/ -->
    </div>
</script>
<script type="text/javascript">
    $(document).ready(function(){
 
    	var dsGrid = new kendo.data.DataSource({
			
            type: "json",
            transport: {
                read: function (options) {
                    $.ajax({
                        type: "POST",
                        dataType: "json",
                        url: "<?php echo $view->url(array('controller' => 'ExpenditureTypes', 'action' => 'getListExpenditureTypes')); ?>",        
							success: function (resultado){
                            options.success(resultado);
                        	}
                    	});
                	}
            		},
            
					schema: {
                	model: {
                    id: "expenditureTypeID",
                    fields: {
							 id: {type:"number"}, 	
	                         name: { type: "string"},
	                         description: { type: "string" },
							 companie: {type: "string"},
							 date: {type: "DateTime"}
                    }
                }
            },
            pageSize: 10
        });

        $("#grid").kendoGrid({
            dataSource: dsGrid,
            height: 430,
            scrollable: true,
            filterable: {
                extra: false
            },
            toolbar: kendo.template($("#template").html()),
            pageable: {
                refresh: true,
                pageSizes: true,
                buttonCount: 10
            },
            selectable: "row",
            columns: [
			    { field: "id", title: "ID", width: "50px" },
                { field: "name", title: "Nombre", width: "200px" },
                { field: "description", title: "Descripción", width: "200px" },
				{ field: "companie", title: "Compañía", width: "200px" },
				{ field: "date", title: "Fecha", width: "100px" },
				{ field: "", template: "<input type='button' class='k-button' name='btnedit' onclick='editExpenditureTypes()' value='Editar' /> <input type='button' class='k-button' name='delExpenditureTypes' onclick='delExpenditureTypes()' value='Eliminar' />",  width: "200px" }
		]
        });
		
		 var viewModel = kendo.observable({
            expendituretypeId: 0,
            new: function(){
                this.set("expendituretypeId", 0);
                window.open("<?php echo $view->url(array('controller' => 'ExpenditureTypes', 'action' => 'formExpenditureTypes')); ?>" , "_self");
				} 
			  });
        
        kendo.bind($(".contexto"), viewModel);
    });
    
	/* function delExpenditureTypes()
    {
    	 //llamar al id del registro
         var grid = $("#grid").data("kendoGrid");
        if (grid.dataItem(grid.select()) != undefined)
            {
        var idexpenditure = grid.dataItem(grid.select()).expenditureTypeID
            }
			else
        	{
        		alert("No seleccionado")
        		return false;
            }
   
            $.ajax({

              type: "POST",
              data: 'idexpenditure=' + idexpenditure,
              url: "<?php //echo $view->url(array('controller' => 'ExpenditureTypes', 'action' => 'delExpenditureTypes')); ?>",
              success: function (respuesta)
                {
                 alert(respuesta)
				 }
           });
   }*/
	function editExpenditureTypes()
    {
        var grid = $("#grid").data("kendoGrid");
        if (grid.dataItem(grid.select()) != undefined)
            {
        var idexpenditure = grid.dataItem(grid.select()).expenditureTypeID
            }

        else
        	{
        		alert("No seleccionado")
        		return false;
            }
        window.open("<?php echo $view->url(array('controller' => 'ExpenditureTypes', 'action' => 'editExpenditureTypes')).'idexpenditure/'; ?>" + idexpenditure, "_self");
    }
</script>
